-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
28 lines (24 loc) · 776 Bytes
/
Solution.java
File metadata and controls
28 lines (24 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.util.Scanner;
public class PrimeOrComposite {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
boolean isPrime = true;
if (num <= 1) {
System.out.println("The number is neither prime nor composite.");
} else {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println("The number is prime.");
} else {
System.out.println("The number is composite.");
}
}
}
}